<?php
namespace Siter\Controller;

class IndexController extends Controller {
	
	public function index() {
		$m = D2();
		$page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();
		$this->assign('page',$page);
        $this->display();
    }
    public function doPasswd() {
        $oldPasswd = I('old_passwd' , '');
        $passwd    = I('passwd'     , '');
        $uid       = C('RUN_LOGIN_USER.uid');
        $m         = D2('User');

        try {
            $r = $m->changePasswd($uid, $oldPasswd, $passwd)
                 ? array('s' => true, 'm' => '密码修改成功')
                 : array('s' => false, 'm' => '密码修改出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }
	
    public function getUserInfo() {
        $uid = C('RUN_LOGIN_USER.uid');

        if(!$record = D2('User')->getByUid($uid)) {
            showJson(array('s' => false, 'm' => '没有找到会员信息'));
        }

        $field = array('user_name', 'qq', 'email', 'tel', 'reg_time', 'bank', 'bankname', 'bankacc', 'accountname', 'last_login_time');
        $r     = array();

        foreach($field as $v) {
            $r[$v] = $record[$v];
        }

        $r['bank2'] = getBankNameCn($r['bank']);

        showJson(array('s' => true, 'd' => $r));
    }

    /**
     * getLast7DayFlow
     * 最近7天结算量
     *
     * @return void
     */
    public function getLast7DayFlow() {
        $r = array();
        $m = D2('VSiterCounter');

        for($i = 1; $i < 8; $i++) {
            $timestamp = strtotime("-{$i} day");
            $day  = date('Y-m-d', $timestamp);
            $expr = array('day' => $day);
            $tmp  = ($tmp = $m->where($expr)->select()) ? $tmp : null;

            if(empty($tmp)) {
                $tmp2 = array(
                    'CPC' => 0,
                    'CPM' => 0,
                    'CPV' => 0,
                );
            } else {
                foreach(array('CPC', 'CPM', 'CPV') as $v) {
                    foreach($tmp as $v2) {
                        if(strtoupper($v2['plan_type']) == $v) {
                            $tmp2[$v] = $v2['counter'];
                        }
                    }

                    if(!isset($tmp2[$v])) {
                        $tmp2[$v] = 0;
                    }
                }
            }

            $r[date('m-d', $timestamp)] = $tmp2;
        }

        showJson(array('s' => true, 'd' => $r));
    }
	
}
